<?php

class Admin_Model_User extends Zend_Db_Table {

  protected $_name = 'users';
  protected $_primary = 'id';

  public function listItem($arrParam = null, $options = null) {
    $result = array();
    $db = Zend_Registry::get('connectDb');
    // $db = Zend_Db::factory ( $adapter, $config );
    $paginator = $arrParam['paginator'];
    $ssFilter = $arrParam['ssFilter'];

    if ($options['task'] == 'user-list') {
      $select = $db->select()->from('users as u', array('id', 'user_name', 'email', 'status', 'register_date'))->joinLeft('user_group as g', 'g.id = u.group_id', 'group_name');

      if (!empty($ssFilter['col']) && !empty($ssFilter['order'])) {
        $select->order($ssFilter['col'] . ' ' . $ssFilter['order']);
      }

      if ($paginator['itemCountPerPage'] > 0) {
        $page = $paginator['currentPage'];
        $rowCount = $paginator['itemCountPerPage'];
        $select->limitPage($page, $rowCount);
      }
      if (!empty($ssFilter['keywords'])) {
        $keywords = '%' . $ssFilter['keywords'] . '%';
        $select->where('u.user_name LIKE ?', $keywords);
      }

      if ($ssFilter['group_id'] > 0) {
        $select->where('u.group_id = ?', $ssFilter['group_id']);
      }
      $result = $db->fetchAll($select);

    }
    return $result;
  }

  public function countItem($arrParam = null, $options = null) {
    $db = Zend_Registry::get('connectDb');
    // $db = Zend_Db::factory ( $adapter, $config );
    $ssFilter = $arrParam['ssFilter'];
    $select = $db->select()->from('users as u', array('COUNT(u.id) as totalItem'));

    if (!empty($ssFilter['keywords'])) {
      $keywords = '%' . $ssFilter['keywords'] . '%';
      $select->where('u.user_name LIKE ?', $keywords);
    }
    if ($ssFilter['group_id'] > 0) {
      $select->where('u.group_id = ?', $ssFilter['group_id']);
    }

    $result = $db->fetchOne($select);
    return $result;

  }

  public function saveItem($arrParam = null, $options = null) {

    if ($options['task'] == 'user-add') {
      $row = $this->fetchNew();
      $encode = new Zendvn_Encode();
      $row->user_name = $arrParam['user_name'];
      $row->user_avatar = $arrParam['user_avatar'];
      $row->password = $encode->password($arrParam['password']);
      $row->email = $arrParam['email'];
      $row->group_id = $arrParam['group_id'];
      $row->first_name = $arrParam['first_name'];
      $row->last_name = $arrParam['last_name'];
      $row->birthday = $arrParam['birthday'];
      $row->status = $arrParam['status'];
      $row->sign = $arrParam['sign'];
      $row->register_date = date("Y-m-d");
      $row->register_ip = $_SERVER['REMOTE_ADDR'];

      $row->save();
    }

    if ($options['task'] == 'user-edit') {

      $where = 'id = ' . $arrParam['id'];

      $row = $this->fetchRow($where);
      $encode = new Zendvn_Encode();
      $row->user_name = $arrParam['user_name'];

      if (!empty($arrParam['password'])) {
        $row->password = $encode->password($arrParam['password']);
      }
      $row->email = $arrParam['email'];
      $row->group_id = $arrParam['group_id'];
      $row->first_name = $arrParam['first_name'];
      $row->last_name = $arrParam['last_name'];
      $row->birthday = $arrParam['birthday'];
      $row->status = $arrParam['status'];
      $row->sign = $arrParam['sign'];
      $row->user_avatar = $arrParam['user_avatar'];

      $row->save();
    }

  }

  public function getItem($arrParam = null, $options = null) {

    if ($options['task'] == 'user-info' || $options['task'] == 'user-edit') {
      $db = Zend_Registry::get('connectDb');
      // $db = Zend_Db::factory($adapter,$config);
      $select = $db->select()->from('users as u')->joinLeft('user_group as g', 'u.group_id = g.id', array('group_name'))->where('u.id = ?', $arrParam['id']);

      $result = $db->fetchRow($select);
    }
    if ($options['task'] == 'delete') {
      $where = 'id = ' . $arrParam['id'];
      $result = $this->fetchRow($where)->toArray();
    }

    return $result;
  }

  public function deleteItem($arrParam = null, $options = null) {
    if ($options['task'] == 'user-delete') {

      //----------LAY TEN HINH ANH user_avatar --------------
      $row = $this->getItem($arrParam, array('task' => 'delete'));

      //--------------XOA user_avatar -----------------------
      $upload_dir = UPLOADS_PATH . '/users';
      $upload = new Zendvn_File_Upload();
      $upload->removeFile($upload_dir . '/orignal/' . $row['user_avatar']);
      $upload->removeFile($upload_dir . '/img100x100/' . $row['user_avatar']);
      $upload->removeFile($upload_dir . '/img450x450/' . $row['user_avatar']);

      $where = ' id = ' . $arrParam['id'];
      $this->delete($where);
    }

    if ($options['task'] == 'user-multi-delete') {
      $cid = $arrParam['cid'];

      if (count($cid) > 0) {

        foreach ($cid as $key) {
          $arrParam['id'] = $key;

          //----------LAY TEN HINH ANH user_avatar --------------
          $row = $this->getItem($arrParam, array('task' => 'delete'));

          //--------------XOA user_avatar -----------------------
          $upload_dir = UPLOADS_PATH . '/users';
          $upload = new Zendvn_File_Upload();
          $upload->removeFile($upload_dir . '/orignal/' . $row['user_avatar']);
          $upload->removeFile($upload_dir . '/img100x100/' . $row['user_avatar']);
          $upload->removeFile($upload_dir . '/img450x450/' . $row['user_avatar']);
        }
        $ids = implode(',', $cid);
        $where = 'id IN (' . $ids . ')';
        $this->delete($where);
      }
    }
  }

  public function changeStatus($arrParam = null, $options = null) {
    $cid = $arrParam['cid'];
    if (count($cid) > 0) {
      if ($arrParam['type'] == 1) {
        $status = 1;
      } else {
        $status = 0;
      }
      $ids = implode(',', $cid);
      $data = array('status' => $status);
      $where = 'id IN (' . $ids . ')';
      $this->update($data, $where);
    }

    if ($arrParam['id'] > 0) {
      if ($arrParam['type'] == 1) {
        $status = 1;
      } else {
        $status = 0;
      }

      $data = array('status' => $status);
      $where = 'id = ' . $arrParam['id'];
      $this->update($data, $where);
    }
  }

  public function sortItem($arrParam = null, $options = null) {

    $cid = $arrParam['cid'];
    $order = $arrParam['order'];
    if (count($cid) > 0) {

      foreach ($cid as $key => $val) {
        $where = ' id = ' . $val;
        $data = array('order' => $order[$val]);
        $this->update($data, $where);
      }
    }

  }
}
